package com.terren.spider.util.common;

import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Random;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.terren.spider.entity.config.SysConfig;




/**
 * 常用工具类
 * 
 * @author Administrator
 *
 */
public class CommonUtil {

	
	private static ClassPathXmlApplicationContext context;
	private static String[] springConfig = { "config/spring/applicationContext.xml" };
	
	/**
	 * 获取spring bean
	 * @param name
	 * @return
	 */
	public static Object getBean(String name){
		if (null == context) {
			context = new ClassPathXmlApplicationContext(springConfig);
		}
		return context.getBean(name);
	}
	/**
	 * 获取spring bean
	 * @param requiredType
	 * @return
	 */
	public static <T> T getBean(Class<T> requiredType) {
		if (null == context) {
			context = new ClassPathXmlApplicationContext(springConfig);
		}
		return context.getBean(requiredType);
	}
	/**
	 * 获得hashId
	 * 
	 * @param text
	 * @return
	 */
	public static long getHashId(String text) {
		long resultId = Lookup3Hash.toID(text);
		return resultId;
	}

	public static <T> String getProjectPath(Class<T> cls) {
		String filePath = null;
		try {
			URL url = cls.getProtectionDomain().getCodeSource().getLocation();
			filePath = URLDecoder.decode(url.getPath(), "utf-8");
			if (filePath.endsWith(".jar")) {
				filePath = filePath.substring(0, filePath.lastIndexOf("/") + 1);
			}
			File file = new File(filePath);
			filePath = file.getAbsolutePath();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return filePath;
	}

	public static <T> String getRealPath(Class<T> cls) {
		String realPath = cls.getClassLoader().getResource("").getFile();
		File file = new File(realPath);
		realPath = file.getAbsolutePath();
		try {
			realPath = URLDecoder.decode(realPath, "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return realPath;
	}
	
	/**
	 * 
	 * @return
	 */
	public static Properties getSysConfigProperties() {
		InputStream resourceAsStream = CommonUtil.class.getClassLoader()
                .getResourceAsStream(SysConfig.CONFIG_FILE_NAME);
        if (resourceAsStream == null) {
            LogUtil.logger.error("没有找到配置文件:"+SysConfig.CONFIG_FILE_NAME);
            return null;
        }
        Properties properties = new Properties();
        try {
            properties.load(resourceAsStream);
        }catch (Exception e) {
        	LogUtil.logger.error("读取配置文件错误:"+SysConfig.CONFIG_FILE_NAME,e);
		}
        return properties;
	}
	/**
	 * 获得唯一ID
	 * 
	 * @return
	 */
	public static String getSingleId() {
		String formatType = "yyyyMMddHHmmsssss";
		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat(formatType);

		String singleId = format.format(date);

		int ran = getRandomInt(0, 1000);

		singleId = singleId + "-" + ran;

		return singleId;
	}
	/**
	 * 获得随机数
	 * 
	 * @param minInt
	 * @param maxInt
	 * @return
	 */
	public static int getRandomInt(int minInt, int maxInt) {
		int number = maxInt - minInt + 1;

		Random random = new Random();

		int ranNum = random.nextInt(number) + minInt;// 0-5

		return ranNum;
	}
}
